GoLang

Explain Methods in Go Programming Language | Go Programming Tutorial

Explain Methods in Go Programming Language | Go Programming Tutorial

Explain Methods in Go Programming Language | Go Programming Tutorial

In this article, you will learn:

  • What are Methods in Go Programming?
  • Elaborate Method with struct type receiver
  • Elaborate Method with Non-Struct Type Receiver
  • Elaborate Methods with Pointer Receiver
  • Difference Between Method and Function

What are Methods in Go Programming Language?

Go language supports the concept of methods. Go methods are alike to the Go function with one difference i.e the method contains a receiver argument in it. Through receiver argument, the methods can access the fields of the receiver. Here, the receiver can be of struct and non-struct type. When you create a method in your code the receiver and the type of the receiver must be available in the same package. The user is not allowed to define a method in which the receiver type is already declared in different packages including inbuilt types like string, int, etc. If the user tries to do so, then the compiler will give you an error. 

SYNTAX:

func(receiver_name Type) method_name(parameters_list) (return_type) {

 // set of statements

}

Here, the receiver is accessed inside the method.

Method with struct type receiver

In GoLang, the user is allowed to declare a method whose receiver is of a struct type. 

package main
import "fmt"
 
// Student_info structure
type student_info struct {
    name      string
    branch    string
     roll_no   int
}
 
// Method with a receiver of student_info type
func (s student_info) show() {
 
    fmt.Println("Student's Name: ", s.name)
    fmt.Println("Branch Name: ", s.branch)
    fmt.Println("Student's roll_no ", s.roll_no)
}
 
// Main function
func main() {
 
    // Initializing the values of the student_info structure
    stu := student_info{
        name:      "John",
        branch:    "CSE",
        roll_no:    56U01,
    }
   stu.show()
}

OUTPUT:

Student's Name: John

Branch: CSE

Student's roll_no: 56U01

Method with non-struct type receiver

In GoLang, the user is allowed to create a method with non-struct type receiver as long as the type and the method definitions are available in the same package. If they are present in different packages like int, string, etc, then the compiler will give the user an error message because it is defined in the different package.

Example:

package main
import "fmt"
 
// Type definition
type data int
 
// Defining a method withnon-struct type receiver
func (m1 data) mul(m2 data) data {
    return m1 * m2
}
 
/*
// if you try to run this code,then compiler will throw an error
func(m1 int)mul(m2 int)int{
return m1 * m2
}
*/
 
// Main function
func main() {
    v_1 := data(30)
    v_2 := data(20)
    res := v_1.mul(v_2)
    fmt.Println("Result: ", res)
}

OUTPUT:

Result: 600

Methods with the Pointer Receiver

In GoLang, the user can create a method with a pointer receiver. With the help of a pointer receiver, if any change happened in the method will directly reflect in the caller which is not possible with the value of the receiver methods.

Difference between Method and Function:

Methods:

  1. It contain a receiver
  2. Methods of the same name but with the different data types are defined in the program
  3. Methods can not be used as a first-order object.

Functions:

  1. It does not contain a receiver.
  2. Functions of the same name but declared with different data types are not allowed in the program.
  3. Functions can be used as First-order objects and can be passed as arguments.
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.